﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using autobackup.util;
using ZTO.PicTest.Utilities;
using System.Collections;

namespace autobackup.job
{
    class BackUpJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {

            Form1.instance.logMain("正在执行任务");


            JobDataMap dataMap = context.JobDetail.JobDataMap;
            string pathFormStr = dataMap.GetString("pathFroms");
            if (pathFormStr == null) {

                Form1.instance.logMain("未获取要备份的文件夹目录");
                return;
            }
            string[] pathForms = pathFormStr.Split(new string[] { Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

            string pathToStr = dataMap.GetString("pathTo");
           
            if (pathToStr == null)
            {
                Form1.instance.logMain("未获取备份的文件夹目录");
                return;
            }

            //复制文件
            //FileUtil.startMutilCopy(pathForms, pathTo);
            string[] to = pathToStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            ZipHelper zip = new ZipHelper();
            if (pathForms.Length == to.Length)
            {
                for (int i = 0; i < pathForms.Length; i++)
                {
                    ///FileUtil.startCopy(pathForms[i], to[i]);

                    Hashtable ht = zip.GetAllFies(pathForms[i]);
                    string backdir = to[i];
                    foreach (string key in ht.Keys)
                    {
                        //Console.WriteLine(key);
                        if (Form1.instance.isCompressFileCheckedChanged())
                        {
                            string aFirstName = key.Substring(key.LastIndexOf("\\") + 1, (key.LastIndexOf(".") - key.LastIndexOf("\\") - 1));
                            string file = backdir + "\\" + aFirstName + ".zip";
                            zip.ZipFile(key, file);
                            FileUtil.total++;
                        }
                        else
                        {
                            string file = backdir + "\\" + FileUtil.GetFileName(key);
                            FileUtil.Copy(key, file);
                        }

                        if (Form1.instance.isDelFileCheckedChanged())
                        {
                            FileUtil.DeleteFile(key);
                        }

                    }
                    
                }

            }
            //更新form上面的显示信息
            Form1.instance.showJob();
            Form1.instance.logMain("本次任务共处理文件 " + FileUtil.total + " 个, 不需要处理的文件 " + FileUtil.noCopy + " 个, 删除文件 " + FileUtil.delcount + " 个");

            Console.WriteLine("Hello " + DateTime.Now.ToString());
        }
    }
}
